package com.android.lmy;

import android.app.Application;
import android.database.sqlite.SQLiteDatabase;

import com.android.lmy.greendao.DaoMaster;
import com.android.lmy.greendao.DaoSession;
import com.jeremyliao.liveeventbus.LiveEventBus;

import org.litepal.LitePal;

/**
 * @功能:
 * @Creat 2020/1/8 11:15
 * @User Lmy
 * @Compony JinAnChang
 */
public class App extends Application {
    private DaoSession daoSession;
    public static App instances;


    @Override
    public void onCreate() {
        super.onCreate();
        instances = this;
        LitePal.initialize(this);
        initDreenDao();
        initLiveEventBus();
    }

    public static App getInstances() {
//        if(instances==null){}
        return instances;
    }


    /**
     * 初始化LiveEventBus
     * 1、supportBroadcast配置支持跨进程、跨APP通信
     * 2、配置LifecycleObserver（如Activity）接收消息的模式（默认值true）：
     * true：整个生命周期（从onCreate到onDestroy）都可以实时收到消息
     * false：激活状态（Started）可以实时收到消息，非激活状态（Stoped）无法实时收到消息，需等到Activity重新变成激活状
     * 态，方可收到消息
     * 3、autoClear
     * 配置在没有Observer关联的时候是否自动清除LiveEvent以释放内存（默认值false）
     */
    private void initLiveEventBus() {
        LiveEventBus.
                config().
                supportBroadcast(this).
                lifecycleObserverAlwaysActive(true).
                autoClear(false);
    }


    /**
     * greendao数据库初始化
     */
    private void initDreenDao() {
        DaoMaster.DevOpenHelper devOpenHelper = new DaoMaster.DevOpenHelper(this, "zbc_test.db");
        SQLiteDatabase db = devOpenHelper.getWritableDatabase();
        DaoMaster daoMaster = new DaoMaster(db);
        daoSession = daoMaster.newSession();
    }


    /**
     * 获取 DaoSession
     *
     * @return
     */
    public DaoSession getDaoSession() {
        return daoSession;
    }
}
